package com.laulkar.activities;

import java.text.DateFormat;
import java.util.Date;
import java.util.LinkedList;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.laulkar.Constants;
import com.laulkar.R;
import com.laulkar.dataproviders.AlarmDbAdapter;

public class TimeSelector extends Activity {

	LinearLayout selected_lines;
	LinkedList<String> selected;
	Date start_date;
	Date end_date;
	
	TextView start;
	TextView end;

	static final int TIME_DIALOG_START_ID = 0;
	static final int TIME_DIALOG_END_ID = 1;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selector);
		selected_lines = (LinearLayout)findViewById(R.id.selected_lines);

		selected = new LinkedList<String>();

		start = (TextView) findViewById(R.id.start_time);
		end = (TextView) findViewById(R.id.end_time);

		start_date = new Date();
		end_date = new Date();
		
		setTimerListners();
		setGalleryListners();
		setButtonListners();
		setDates();

	}

	private void setGalleryListners() {
		Gallery g = (Gallery) findViewById(R.id.selector_gallery);

		//setDisplay();
		g.setAdapter(new ImageAdapter(this));

		// Set a item click listener, and just Toast the clicked position
		g.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView parent, View v, int position, long id) {
				if(!selected.contains(Constants.mLineNames[position]))
				{

					
					ImageView iv = new ImageView(TimeSelector.this);
					iv.setImageResource(Constants.subway_icons.get(Constants.mLineNames[position]));
					selected_lines.addView(iv);
					Toast.makeText(TimeSelector.this, "" + position, Toast.LENGTH_SHORT).show();
					selected.add(new String(Constants.mLineNames[position]));
					//parent.removeViewInLayout(v);
					//parent.refreshDrawableState();
				}
			}
		});
		// We also want to show context menu for longpressed items in the gallery

		registerForContextMenu(g);
	}

	private void setButtonListners() {
		Button b = (Button) findViewById(R.id.selector_done);
		b.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				addAlarm();
				finish();
			}
		});
		

	}

	private void addAlarm()
	{
		if(selected.isEmpty())
		{
			finish();
		}
		AlarmDbAdapter db = new AlarmDbAdapter(this);
		db.open();
		String lines = new String();
		for(String s: selected)
		{
			if(lines.length() > 0)
				lines = lines + "," + s;
			else
				lines = s;
		}
		
		db.createAlarm(lines, convertDate(start_date), convertDate(end_date));
		db.close();
	}
	
	public int convertDate(Date date)
	{
		return date.getHours() * 100 + date.getMinutes();
	}
	private void setTimerListners() {
		start.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(TIME_DIALOG_START_ID);
			}
		});
		end.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(TIME_DIALOG_END_ID);
			}
		});
	}

	private void setDates()
	{
		start.setText(DateFormat.getTimeInstance(DateFormat.SHORT).format(start_date));
		end.setText(DateFormat.getTimeInstance(DateFormat.SHORT).format(end_date));
		
	}
	
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case TIME_DIALOG_START_ID:
			return new TimePickerDialog(this,mstart_Dialog, 0, 0, false);
		case TIME_DIALOG_END_ID:
			return new TimePickerDialog(this,mend_Dialog, 0, 0, false);
		}
		return null;
	}

	public void onDestroy()
	{
		super.onDestroy();

	}
	private OnClickListener mStartSubwaySelector = new OnClickListener() {
		public void onClick(View v) {
			finish();

		}
	};

	/* ImageAdapter for the gallery */
	public class ImageAdapter extends BaseAdapter {
		int mGalleryItemBackground;

		public ImageAdapter(Context c) {
			mContext = c;
			// See res/values/attrs.xml for the <declare-styleable> that defines
			// Gallery1.
			TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
			mGalleryItemBackground = a.getResourceId(
					R.styleable.Gallery1_android_galleryItemBackground, 0);
			a.recycle();
		}

		public int getCount() {
			return Constants.mLineNames.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i = new ImageView(mContext);

			i.setImageResource(Constants.subway_icons.get(Constants.mLineNames[position]));
			i.setScaleType(ImageView.ScaleType.FIT_XY);
			i.setLayoutParams(new Gallery.LayoutParams(200, 80));

			// The preferred Gallery item background
			i.setBackgroundResource(mGalleryItemBackground);

			return i;
		}

		private Context mContext;


	}

	/* Timer Dialog for the start and end Dialogs */
	private TimePickerDialog.OnTimeSetListener mstart_Dialog =
		new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			//Toast.makeText(ExampleApp.this, "Time is="+hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
			
			start_date.setHours(hourOfDay);
			start_date.setMinutes(minute);
			setDates();

		}
	};

	private TimePickerDialog.OnTimeSetListener mend_Dialog =
		new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			
			end_date.setHours(hourOfDay);
			end_date.setMinutes(minute);
			setDates();

		}
	};

}
